%% combine into one file for the connected countries in Europe

clear
close all
clc

% load list of connected countries
folders;
country_list_connected;

% append the files
for country_n=1:Ncountries
    
    country_icc = char( countries_connected (country_n) );  % country ICC code
    country = icc2name( country_icc );           % country name

    if ~ispc()   %% we are in a mac

        % folder to save the outcome
        datafolder_LAC_country = [ datafolder_LAC,country_icc,'/'];

    else   %% we are in a pc   

        % folder to save the outcome
        datafolder_LAC_country = [ datafolder_LAC,country_icc,'\' ];

    end
    
    
    clc
    disp( ['Country: ',country] )    
    
    % load sedac
    file  = [ datafolder_LAC_country,'popplaces_sedac.mat'];
    temp = load(file); 
    
    % save temp file
    places_sedac_temp{country_n} = temp.places_sedac;
    for i=1:length(places_sedac_temp{country_n})
        places_sedac_temp{country_n}(i).country = country;
    end
    
end

i=1;
for country_n=1:Ncountries
    for j=1:length( places_sedac_temp{country_n} )
        places_sedac(i).Geometry =  places_sedac_temp{country_n}(j).Geometry;
        places_sedac(i).X =  places_sedac_temp{country_n}(j).X;
        places_sedac(i).Y =  places_sedac_temp{country_n}(j).Y;
        places_sedac(i).population =  places_sedac_temp{country_n}(j).population;
        places_sedac(i).country =  places_sedac_temp{country_n}(j).country;
        i=i+1;
    end
end

% save
if strcmp(country_names_connected(1,2), 'BO')
    datafolder_connected_countries =  './connected_countries_data2/'  ;
end

% save
save([datafolder_connected_countries,'popplaces_sedac.mat'],'places_sedac');

